package org.carabiner.examples;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * A random applet pulled from the web
 * 
 * http://www.clubmaker-online.com/dt.swingweight.html
 * 
 * @version 0.3
 */
public class MainApplet extends Applet
{
  private String[] LORY_LETTERS = { "A", "B", "C", "D", "E", "F", "G", "H",
      "I", "J", "K" };

  private static final int AZERO_OFFSET = 95;

  private boolean isStandalone = false;

  JPanel jPanel1 = new JPanel();

  JPanel jPanel2 = new JPanel();

  JPanel jPanel3 = new JPanel();

  JPanel jPanel4 = new JPanel();

  JPanel jPanel5 = new JPanel();

  JPanel jPanel6 = new JPanel();

  JLabel jLabel1 = new JLabel();

  JLabel jLabel2 = new JLabel();

  JLabel swingweightLabel = new JLabel();

  JLabel jLabel3 = new JLabel();

  JLabel jLabel4 = new JLabel();

  JSlider lengthSlider = new JSlider();

  JLabel lengthLabel = new JLabel();

  JSlider headWeightSlider = new JSlider();

  JLabel headWeightLabel = new JLabel();

  JSlider shaftWeightSlider = new JSlider();

  JLabel shaftWeightLabel = new JLabel();

  JLabel jLabel6 = new JLabel();

  JSlider gripWeightSlider = new JSlider();

  JLabel gripWeightLabel = new JLabel();

  FlowLayout flowLayout1 = new FlowLayout();

  FlowLayout flowLayout2 = new FlowLayout();

  FlowLayout flowLayout3 = new FlowLayout();

  FlowLayout flowLayout4 = new FlowLayout();

  FlowLayout flowLayout5 = new FlowLayout();

  JLabel jLabel5 = new JLabel();

  JLabel totalWeightLabel = new JLabel();

  JPanel jPanel7 = new JPanel();

  JLabel jLabel7 = new JLabel();

  JLabel moiLabel = new JLabel();

  JPanel jPanel8 = new JPanel();

  JLabel jLabel8 = new JLabel();

  JSlider shaftLenSlider = new JSlider();

  JLabel shaftLenLabel = new JLabel();

  // Get a parameter value
  public String getParameter(String key, String def)
  {
    return isStandalone ? System.getProperty(key, def)
        : (getParameter(key) != null ? getParameter(key) : def);
  }

  // Construct the applet
  public MainApplet()
  {
  }

  // Initialize the applet
  public void init()
  {
    try
    {
      jbInit();
      ChangeListener l = new ChangeListener()
      {
        public void stateChanged(ChangeEvent e)
        {
          update();
        }
      };
      shaftLenSlider.addChangeListener(l);
      lengthSlider.addChangeListener(l);
      shaftWeightSlider.addChangeListener(l);
      gripWeightSlider.addChangeListener(l);
      headWeightSlider.addChangeListener(l);
    } catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  // Component initialization
  private void jbInit() throws Exception
  {
    setLayout(new GridLayout(8, 1));
    jLabel1.setRequestFocusEnabled(true);
    jLabel1.setText("Club Length (Inches)");
    jPanel1.setLayout(flowLayout4);
    jPanel6.setLayout(flowLayout5);
    jLabel2.setFont(new java.awt.Font("Dialog", 0, 20));
    jLabel2.setText("Swingweight:");
    swingweightLabel.setFont(new java.awt.Font("Dialog", 0, 20));
    swingweightLabel.setBorder(BorderFactory.createRaisedBevelBorder());
    swingweightLabel.setMaximumSize(new Dimension(50, 30));
    swingweightLabel.setMinimumSize(new Dimension(50, 30));
    swingweightLabel.setPreferredSize(new Dimension(50, 30));
    swingweightLabel.setToolTipText("");
    swingweightLabel.setHorizontalAlignment(SwingConstants.CENTER);
    swingweightLabel.setText("D7");
    jLabel3.setText("Head Weight (grams)");
    jLabel3.setRequestFocusEnabled(true);
    jPanel2.setLayout(flowLayout3);
    jLabel4.setText("Uncut Shaft Weight (g)");
    jPanel3.setLayout(flowLayout1);
    lengthLabel.setFont(new java.awt.Font("Dialog", 1, 15));
    lengthLabel.setMaximumSize(new Dimension(60, 20));
    lengthLabel.setMinimumSize(new Dimension(60, 15));
    lengthLabel.setPreferredSize(new Dimension(60, 20));
    lengthLabel.setRequestFocusEnabled(true);
    lengthLabel.setText("40.0\"");
    lengthSlider.setMaximum(400);
    lengthSlider.setMinimum(240);
    lengthSlider.setValue(40 * 8);
    lengthSlider.setMinorTickSpacing(16);
    lengthSlider.setPaintLabels(false);
    lengthSlider.setPaintTicks(true);
    lengthSlider.setMinimumSize(new Dimension(36, 24));
    lengthSlider.addChangeListener(new MainApplet_lengthSlider_changeAdapter(
        this));
    headWeightSlider.setMajorTickSpacing(50);
    headWeightSlider.setMaximum(350);
    headWeightSlider.setMinimum(150);
    headWeightSlider.setValue(250);
    headWeightSlider.setMinorTickSpacing(25);
    headWeightSlider.setPaintLabels(true);
    headWeightSlider.setPaintTicks(true);
    headWeightSlider
        .addChangeListener(new MainApplet_headWeightSlider_changeAdapter(this));
    headWeightLabel.setFont(new java.awt.Font("Dialog", 1, 15));
    headWeightLabel.setText("250");
    shaftWeightLabel.setFont(new java.awt.Font("Dialog", 1, 15));
    shaftWeightLabel.setText("110");
    jPanel3.setMinimumSize(new Dimension(173, 34));
    shaftWeightSlider.setMajorTickSpacing(15);
    shaftWeightSlider.setMaximum(150);
    shaftWeightSlider.setMinimum(50);
    shaftWeightSlider.setMinorTickSpacing(5);
    shaftWeightSlider.setPaintLabels(true);
    shaftWeightSlider.setPaintTicks(true);
    shaftWeightSlider.setValue(110);
    shaftWeightSlider
        .addChangeListener(new MainApplet_shaftWeightSlider_changeAdapter(this));
    jLabel6.setText("Grip Weight (grams)");
    jPanel4.setLayout(flowLayout2);
    gripWeightLabel.setFont(new java.awt.Font("Dialog", 1, 15));
    gripWeightLabel.setText("50");
    gripWeightSlider.setMajorTickSpacing(25);
    gripWeightSlider.setMaximum(100);
    gripWeightSlider.setMinimum(25);
    gripWeightSlider.setMinorTickSpacing(5);
    gripWeightSlider.setPaintLabels(true);
    gripWeightSlider.setPaintTicks(true);
    gripWeightSlider
        .addChangeListener(new MainApplet_gripWeightSlider_changeAdapter(this));
    jLabel5.setText("Total Weight:");
    jLabel5.setFont(new java.awt.Font("Dialog", 0, 20));
    jLabel5.setToolTipText("");
    totalWeightLabel.setText("410");
    totalWeightLabel.setToolTipText("");
    totalWeightLabel.setHorizontalAlignment(SwingConstants.CENTER);
    totalWeightLabel.setPreferredSize(new Dimension(50, 30));
    totalWeightLabel.setMinimumSize(new Dimension(50, 30));
    totalWeightLabel.setMaximumSize(new Dimension(50, 30));
    totalWeightLabel.setBorder(BorderFactory.createRaisedBevelBorder());
    totalWeightLabel.setFont(new java.awt.Font("Dialog", 0, 20));
    jLabel7.setText("MOI:");
    jLabel7.setFont(new java.awt.Font("Dialog", 0, 20));
    moiLabel.setToolTipText("");
    moiLabel.setHorizontalAlignment(SwingConstants.CENTER);
    moiLabel.setText("");
    moiLabel.setPreferredSize(new Dimension(100, 30));
    moiLabel.setMinimumSize(new Dimension(100, 30));
    moiLabel.setOpaque(false);
    moiLabel.setMaximumSize(new Dimension(100, 30));
    moiLabel.setBorder(BorderFactory.createRaisedBevelBorder());
    moiLabel.setFont(new java.awt.Font("Dialog", 0, 20));
    jLabel8.setText("Uncut Shaft Length");
    shaftLenSlider.setMajorTickSpacing(5);
    shaftLenSlider.setMaximum(50);
    shaftLenSlider.setMinimum(35);
    shaftLenSlider.setMinorTickSpacing(1);
    shaftLenSlider.setPaintLabels(true);
    shaftLenSlider.setPaintTicks(true);
    shaftLenSlider.setValue(41);
    shaftLenSlider
        .addChangeListener(new MainApplet_shaftLenSlider_changeAdapter(this));
    shaftLenLabel.setText("41");
    shaftLenLabel.setRequestFocusEnabled(true);
    shaftLenLabel.setPreferredSize(new Dimension(30, 20));
    shaftLenLabel.setMinimumSize(new Dimension(30, 15));
    shaftLenLabel.setOpaque(false);
    shaftLenLabel.setMaximumSize(new Dimension(30, 20));
    shaftLenLabel.setFont(new java.awt.Font("Dialog", 1, 15));
    this.add(jPanel1, null);
    this.add(jPanel2, null);
    this.add(jPanel3, null);
    jPanel8.add(jLabel8, null);
    jPanel8.add(shaftLenSlider, null);
    jPanel8.add(shaftLenLabel, null);
    this.add(jPanel8, null);
    jPanel4.add(jLabel6, null);
    jPanel4.add(gripWeightSlider, null);
    jPanel4.add(gripWeightLabel, null);
    this.add(jPanel4, null);
    this.add(jPanel5, null);
    jPanel5.add(jLabel5, null);
    jPanel5.add(totalWeightLabel, null);
    this.add(jPanel6, null);
    jPanel6.add(jLabel2, null);
    jPanel6.add(swingweightLabel, null);
    jPanel2.add(jLabel3, null);
    jPanel2.add(headWeightSlider, null);
    jPanel2.add(headWeightLabel, null);
    jPanel1.add(jLabel1, null);
    jPanel1.add(lengthSlider, null);
    jPanel1.add(lengthLabel, null);
    jPanel3.add(jLabel4, null);
    jPanel3.add(shaftWeightSlider, null);
    jPanel3.add(shaftWeightLabel, null);
    this.add(jPanel7, null);
    jPanel7.add(jLabel7, null);
    jPanel7.add(moiLabel, null);

  }

  // Get Applet information
  public String getAppletInfo()
  {
    return "Applet Information";
  }

  // Get parameter info
  public String[][] getParameterInfo()
  {
    return null;
  }

  // Main method
  public static void main(String[] args)
  {
    MainApplet applet = new MainApplet();
    applet.isStandalone = true;
    JFrame frame;
    frame = new JFrame();
    frame.setTitle("Applet Frame");
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    applet.init();
    applet.start();
    frame.setSize(400, 500);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame
        .getSize().height) / 2);
    frame.setVisible(true);
  }

  void lengthSlider_stateChanged(ChangeEvent e)
  {
    lengthLabel.setText(Double.toString(getShaftLength()) + "\"");
  }

  private double getShaftLength()
  {
    return (double) lengthSlider.getValue() * 0.125;
  }

  void headWeightSlider_stateChanged(ChangeEvent e)
  {
    headWeightLabel.setText(Integer.toString(headWeightSlider.getValue()));
  }

  private boolean update()
  {
    // Calculate Swingweight, TotalWeight, and MOI
    int swingweight = (int) ((getShaftLength()
        * (getHeadWeight() + getCutShaftWeight() / 2) - 14
        * (getHeadWeight() + getCutShaftWeight()) - 10 * getGripWeight()) / 50)
        - AZERO_OFFSET;
    int index = (swingweight / 10);
    int remainder = swingweight % 10;
    if (index < 0 || index >= LORY_LETTERS.length)
      return false;
    String loryLetter = LORY_LETTERS[index];
    swingweightLabel.setText(loryLetter + Integer.toString(remainder));

    double moi = getShaftLength() * 2
        * (getHeadWeight() + getCutShaftWeight() / 3);
    moiLabel.setText(Double.toString(moi));

    double totalWeight = getCutShaftWeight() + getHeadWeight()
        + getGripWeight();
    totalWeightLabel.setText(Integer.toString((int) totalWeight));

    return true;
  }

  private double getGripWeight()
  {
    return gripWeightSlider.getValue();
  }

  private double getCutShaftWeight()
  {
    return ((double) shaftWeightSlider.getValue() / (double) shaftLenSlider
        .getValue())
        * getShaftLength();
  }

  private double getHeadWeight()
  {
    return headWeightSlider.getValue();
  }

  void shaftWeightSlider_stateChanged(ChangeEvent e)
  {
    shaftWeightLabel.setText(Integer.toString(shaftWeightSlider.getValue()));
  }

  void gripWeightSlider_stateChanged(ChangeEvent e)
  {
    gripWeightLabel.setText(Integer.toString(gripWeightSlider.getValue()));
  }

  void shaftLenSlider_stateChanged(ChangeEvent e)
  {
    shaftLenLabel.setText(Integer.toString(shaftLenSlider.getValue()));
  }
}

class MainApplet_lengthSlider_changeAdapter implements
    javax.swing.event.ChangeListener
{
  MainApplet adaptee;

  MainApplet_lengthSlider_changeAdapter(MainApplet adaptee)
  {
    this.adaptee = adaptee;
  }

  public void stateChanged(ChangeEvent e)
  {
    adaptee.lengthSlider_stateChanged(e);
  }
}

class MainApplet_headWeightSlider_changeAdapter implements
    javax.swing.event.ChangeListener
{
  MainApplet adaptee;

  MainApplet_headWeightSlider_changeAdapter(MainApplet adaptee)
  {
    this.adaptee = adaptee;
  }

  public void stateChanged(ChangeEvent e)
  {
    adaptee.headWeightSlider_stateChanged(e);
  }
}

class MainApplet_shaftWeightSlider_changeAdapter implements
    javax.swing.event.ChangeListener
{
  MainApplet adaptee;

  MainApplet_shaftWeightSlider_changeAdapter(MainApplet adaptee)
  {
    this.adaptee = adaptee;
  }

  public void stateChanged(ChangeEvent e)
  {
    adaptee.shaftWeightSlider_stateChanged(e);
  }
}

class MainApplet_gripWeightSlider_changeAdapter implements
    javax.swing.event.ChangeListener
{
  MainApplet adaptee;

  MainApplet_gripWeightSlider_changeAdapter(MainApplet adaptee)
  {
    this.adaptee = adaptee;
  }

  public void stateChanged(ChangeEvent e)
  {
    adaptee.gripWeightSlider_stateChanged(e);
  }
}

class MainApplet_shaftLenSlider_changeAdapter implements
    javax.swing.event.ChangeListener
{
  MainApplet adaptee;

  MainApplet_shaftLenSlider_changeAdapter(MainApplet adaptee)
  {
    this.adaptee = adaptee;
  }

  public void stateChanged(ChangeEvent e)
  {
    adaptee.shaftLenSlider_stateChanged(e);
  }
}
